package megamu.mesh;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import quickhull3d.Point3d;
import quickhull3d.QuickHull3D;

import com.jellyfishumbrella.GL.Vector3d;

public class VoronoiHull extends QuickHull3D {
	public VoronoiHull(double[] coords) throws IllegalArgumentException {
		super(coords);
	}

	public VoronoiHull(Point3d[] points) throws IllegalArgumentException {
		super(points);
	}

	public LinkedHashMap<Integer, Region> getVoronoi(VoronoiInput<String, Site> baseSites) {
		Point3d[] verts = getVertices();
		int[][] faces = getFaces(POINT_RELATIVE + CLOCKWISE);
		int[] vertIndices = getVertexPointIndices();
		int numFaces = faces.length;
		LinkedHashMap<Integer, Region> regions = new LinkedHashMap<Integer, Region>();

		Point3d[] faceCenters = new Point3d[numFaces];
		
		for (int i = 0; i < numFaces; i++) {
			int index0 = faces[i][0];
			int index1 = faces[i][1];
			int index2 = faces[i][2];
			
			Vector3d currVert0 = new Vector3d(verts[index0].x, 0.0, verts[index0].y);
			Vector3d currVert1 = new Vector3d(verts[index1].x, 0.0, verts[index1].y);
			Vector3d currVert2 = new Vector3d(verts[index2].x, 0.0, verts[index2].y);

			if((currVert0.x < 6000.0 && currVert0.x > -6000.0) &&
			(currVert0.z < 6000.0 && currVert0.z > -6000.0) &&
			(currVert1.x < 6000.0 && currVert1.x > -6000.0) &&
			(currVert1.z < 6000.0 && currVert1.z > -6000.0) &&
			(currVert2.x < 6000.0 && currVert2.x > -6000.0) &&
			(currVert2.z < 6000.0 && currVert2.z > -6000.0)){
				Site newSite0 = new Site(index0, currVert0);
				Site newSite1 = new Site(index1, currVert1);
				Site newSite2 = new Site(index2, currVert2);
	
				Region newRegion0;
				Region newRegion1;
				Region newRegion2;
				
				if(regions.containsKey(index0)){
					newRegion0 = regions.get(index0);
				}else{
					newRegion0 = new Region(newSite0);
					regions.put(index0, newRegion0);
				}
				
				if(regions.containsKey(index1)){
					newRegion1 = regions.get(index1);
				}else{
					newRegion1 = new Region(newSite1);
					regions.put(index1, newRegion1);
				}
				
				if(regions.containsKey(index2)){
					newRegion2 = regions.get(index2);
				}else{
					newRegion2 = new Region(newSite2);
					regions.put(index2, newRegion2);
				}
	
				newRegion0.addNeighbor(newRegion1);
				newRegion0.addNeighbor(newRegion2);
				
				Corner faceCenter = new Corner(currVert0);
				faceCenter.vert.coord.add(currVert1);
				faceCenter.vert.coord.add(currVert2);
				faceCenter.vert.coord.scale(1.0/3.0);
				
				//newRegion0.addCorner(faceCenter);
				//newRegion1.addCorner(faceCenter);
				//newRegion2.addCorner(faceCenter);
	
				Corner corner0 = new Corner(currVert0);
				Corner corner1 = new Corner(currVert1);
				Corner corner2 = new Corner(currVert2);
	
				newRegion0.addCorner(corner0);
				newRegion0.addCorner(corner1);
				newRegion0.addCorner(corner2);
				newRegion1.addCorner(corner0);
				newRegion1.addCorner(corner1);
				newRegion1.addCorner(corner2);
				newRegion2.addCorner(corner0);
				newRegion2.addCorner(corner1);
				newRegion2.addCorner(corner2);
			}
		}
		return regions;
	}
}
